<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="lib/react.development.js"></script>
    <script src="lib/react-dom.development.js"></script>
    <script src="lib/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel">
    const root = ReactDOM.createRoot(document.querySelector("#root"));
    class App extends React.Component{
        state = {
            userName:"zhangpeiyue",
            age:12,
            sex:"男"
        }
        render(){
            return (
                <div>
                    <button onClick={()=>{
                        this.setState({
                            age:this.state.age+1
                        })
                    }}>更新年龄</button>
                    <button onClick={()=>{
                        this.setState({
                            sex:"女"
                        })
                    }}>更改性别</button>
                    <A sex={this.state.sex} userName={this.state.userName}></A>
                    <hr/>
                    <B age={this.state.age}></B>
                </div>
            )
        }
    }
    // PureComponent:纯组件（只有状态与属性发生改变时才会执行render）
    class A extends React.PureComponent{
        state = {
            num:1
        }
        // shouldComponentUpdate(nextProps,nextState){
        //     // this.props:{userName:"zhangsan",sex:"男"}
        //     // nextProps:{userName:"zhangsan",sex:"男"}
        //     for(let key in nextProps){
        //         console.log(key,nextProps[key],this.props[key])
        //         if(nextProps[key] !== this.props[key]){
        //             return true;
        //         }
        //     }
        //
        //     for(let key in nextState){
        //         if(nextState[key] !== this.state[key]){
        //             return true;
        //         }
        //     }
        //     return false;
        // }
        render(){
            console.log("a->render");
            return (
                <div>
                    <h3>A</h3>
                    <button onClick={()=>{
                        this.setState({
                            num:100
                        })
                    }}>{this.state.num}</button>
                    <p>userName:{this.props.userName}</p>
                    <p>sex:{this.props.sex}</p>
                </div>
            )
        }
    }
    class B extends React.Component{
        render(){
            console.log("b->render");
            return (
                <div>
                    <h3>B</h3>
                    <p>age:{this.props.age}</p>
                </div>
            )
        }
    }
    root.render((
        <App/>
    ))
</script>
</html>